<?php
/**
 * Created by PhpStorm.
 * User: 陈艺赫
 * Date: 2018/5/11
 * Time: 15:49
 */

namespace app\user\model;

use think\Model;
class Menu extends Model
{
    /*
    * @param 用户id
    * @return array 当前用户所有菜单 分级
    * */
    public static function getUserMenus($user_id)
    {
        $group_ids = Group::getGroupIds($user_id);
        $all_menu_list = Group::where(['id' => ['in', $group_ids], 'status' => '1'])
            ->column('menu_ids');
        $temp = implode(',',$all_menu_list);
        $all_menu_ids = array_unique(explode(',',$temp));
        $main_menus = Menu::where(['id' => ['in', $all_menu_ids], 'pid' => 0, 'status' => '1'])
            ->order('sort asc')
            ->select()
            ->toArray();
        foreach ($main_menus as $k => $main_menu) {
            $sub_menu = Menu::where(['id' => ['in', $all_menu_ids], 'pid' => $main_menu['id'], 'status' => '1'])
                ->order('sort asc')
                ->select()
                ->toArray();
            if (!empty($sub_menu)){
                $main_menus[$k]['subset'] = $sub_menu;
            }
        }
        return $main_menus;

    }
}